
let oTabs = document.querySelectorAll(".Tab");
let oContent_Tabs = document.querySelectorAll('.content-Tab');

// 选项卡功能
function bindEvent() {
    for (let i = 0; i < oTabs.length; i++) {
        oTabs[i].addEventListener('click', function () {
            rem();
            oTabs[i].classList.add('active');
            oContent_Tabs[i].classList.add('active');
        })
    }
}
bindEvent();

//移除active属性
function rem() {
    console.log(1);
    for (let i = 0; i < oTabs.length; i++) {
        oTabs[i].classList.remove('active');
        oContent_Tabs[i].classList.remove('active');
    }
}
// 注册
let btn = document.querySelector('.login');
let username = document.getElementById('username');
let password = document.getElementById('password');
let rpassword = document.getElementById('rpassword');
let nickname = document.getElementById('nickname');

btn.onclick = async function () {
    let url = "http://127.0.0.1:8888/users/register";
    let options = {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        body: `username=${username.value}&password=${password.value}&rpassword=${rpassword.value}&nickname=${nickname.value}`
    };

    let response = await fetch(url, options);
    let data = await response.json();
    console.log(data);

    if (data.code === 1) {
        alert("注册成功，马上为您跳转到登录页面");
        setTimeout(function () {
            location.href = "login.html";
        }, 2000)
    } else {
        alert(data.message);
    }
}

let land = document.querySelector('.land');
let land_username = document.querySelector('.land-username');
let land_password = document.querySelector('.land-password');

land.onclick = async function () {
    var options = {
        url: "http://127.0.0.1:8888/users/login",
        type: "POST",
        data: {
            username: land_username.value,
            password: land_password.value
        }
    }
    let data = await ajax_promise(options);
    data = JSON.parse(data);

    switch (data.code) {
        case 0:
            errorTip(data.message);
            break;
        case 1:
            successTip(data.message);

            setTimeout(function () {
                location.href = "../html/home.html"
            }, 2000)
            // 登录成功之后获取存储用的id; 
            setCookie("userid", data.user.id);
            setCookie("token", data.token, {
                expires: 1
            });
            break;
    }
}
var login_tip = document.getElementById("login_tip");

            function errorTip( msg ){
                  login_tip.style.display = "block";
                  login_tip.classList.remove("alert-success");
                  login_tip.classList.add("alert-danger");

                  login_tip.innerHTML = msg;

                  setTimeout(function(){
                        login_tip.style.display = "none";
                  } , 2000)
            }
            function successTip( msg ){
                  login_tip.classList.remove("alert-danger");
                  login_tip.classList.add("alert-success");
                  login_tip.style.display = "block";
                  login_tip.innerHTML = msg;

                  setTimeout(function(){
                        login_tip.style.display = "none";
                  } , 2000)
            }



